﻿Imports System.Web
Imports Mysoft.Map.Data
Imports Mysoft.Map.Application
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Xml
Imports Mysoft.Slxt.Business
Imports System.IO
Imports Mysoft.PubProject.Business

Partial Public Class SetMainMedia_XMLHTTP
    Inherits System.Web.UI.Page
    Dim MyBizParamMNG As New Mysoft.PubProject.Business.BizParamMNG
    Dim MySaleMNG As New Mysoft.Slxt.Business.SaleMNG
    Dim MyRoomMNG As New Mysoft.Slxt.Business.RoomMNG
    Dim MyBankMNG As New Mysoft.Slxt.Business.BankMNG
    Dim MyOrder As New Mysoft.Slxt.Business.Order
    Dim MyPubMng As New Mysoft.PubProject.Business.PublicMNG
    Dim strSQL As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim strYwType As String                 '业务类型
        Dim strOnlyFlag, strTxt As String       'resquest值
        Dim strReturn As String = ""            '返回值

        strYwType = Request.QueryString("ywtype")           '业务标识
        strOnlyFlag = Request.QueryString("ywonlyflag")     '唯一键值
        strTxt = Request.QueryString("ywtxt")               '其它参数值
        strYwType = Request.QueryString("ywtype")

        '接收post的长文本值
        Dim strLongText As String = ""                      'POST文本
        If Request.InputStream.Length > 0 Then
            Dim b(Request.InputStream.Length - 1) As Byte
            Request.InputStream.Read(b, 0, Request.InputStream.Length)
            strLongText = System.Text.Encoding.UTF8.GetString(b)
        End If


        Select Case strYwType.ToUpper
            Case "MainMediaIsQuote".ToUpper                                '效验：判断媒体大类是否已被媒体子类引用
                strReturn = MainMediaIsQuote(strLongText)
                Exit Select
            Case "DeleteMainMedia".ToUpper                                '效验：删除媒体大类
                strReturn = DeleteMainMedia(strLongText)
                Exit Select
            Case "MainMeidaNameIsExist".ToUpper                                '效验：媒体大类名称是否有重复
                strReturn = MainMeidaNameIsExist(strLongText)
                Exit Select
            Case "MainMeidaCodeIsExist".ToUpper                                '效验：媒体大类编码是否有重复
                strReturn = MainMeidaCodeIsExist(strLongText)
                Exit Select
            Case "MainMediaAllIsQuote".ToUpper                                '效验：检查所有的媒体大类中是否有被引用的
                strReturn = MainMediaAllIsQuote(strLongText)
                Exit Select
            Case "ImportDefMainMedia".ToUpper                                '删除并添加默认参数
                strReturn = ImportDefMainMedia(strLongText)
                Exit Select
            Case "UpdateSubMediaCode".ToUpper                                '修改媒体大类同时修改媒体子类中引用对应的的媒体大类编码
                strReturn = UpdateSubMediaCode(strLongText)
                Exit Select
        End Select

        MyBase.Response.ContentType = "text/HTML"
        MyBase.Response.Clear()
        MyBase.Response.Write(strReturn)      ' 返回操作成功信息
        MyBase.Response.End()
    End Sub

#Region "业务处理代码"

    '效验：检查所有的媒体大类中是否有被引用的
    Private Function MainMediaAllIsQuote(ByVal a_strPostValue As String) As String
        Dim strSQL As String = ""
        Dim intCount As Integer
        strSQL = String.Format("SELECT COUNT(1) FROM s_SubMedia WHERE s_SubMedia.MainMediaGUID IN(SELECT MainMediaGUID FROM s_MainMedia)")
        Try
            intCount = MyDB.GetDataItemInt(strSQL)
            If intCount > 0 Then
                Return "NO"
            Else
                Return "OK"
            End If
        Catch ex As Exception
            Return "操作失败.请与管理员联系！"
        End Try
    End Function


    '清空媒体大类并添加默认参数
    Private Function ImportDefMainMedia(ByVal a_strPostValue As String) As String
        Try
            Dim strSQL As String = ""
            '如果 Operate=define 则只删除折扣项，否则同时删除折扣方案和折扣项
            strSQL = String.Format("DELETE s_MainMedia;INSERT INTO s_MainMedia(MainMediaGUID,MainMediaCode,MainMediaName,ThrowType) SELECT MainMediaGUID,MainMediaCode,MainMediaName,ThrowType FROM s_MainMedia_bak")

            If MyDB.ExecSQL(strSQL) > -1 Then
                Return "OK"
            Else
                Return "操作失败.请与管理员联系!"
            End If
        Catch ex As Exception
            Return "操作失败.请与管理员联系!"
        End Try
    End Function


    '效验：媒体大类名称是否有重复
    Private Function MainMeidaNameIsExist(ByVal a_strPostValue As String) As String
        Dim strMainMediaGUID As String = MysRequest.QueryString("oid")
        Dim strSQL As String = ""
        Dim intCount As Integer
        strSQL = String.Format("SELECT COUNT(1) FROM s_MainMedia WHERE MainMediaName='{0}'", a_strPostValue)
        If strMainMediaGUID <> String.Empty Then
            strSQL &= " and MainMediaGUID<>'" + strMainMediaGUID + "'"
        End If
        Try
            intCount = MyDB.GetDataItemInt(strSQL)
            If intCount > 0 Then
                Return "NO"
            Else
                Return "OK"
            End If
        Catch ex As Exception
            Return "操作失败.请与管理员联系！"
        End Try
    End Function

    '效验：媒体大类编码是否有重复
    Private Function MainMeidaCodeIsExist(ByVal a_strPostValue As String) As String
        Dim strMainMediaGUID As String = MysRequest.QueryString("oid")
        Dim strSQL As String = ""
        Dim intCount As Integer
        strSQL = String.Format("SELECT COUNT(1) FROM s_MainMedia WHERE MainMediaCode='{0}'", a_strPostValue)
        If strMainMediaGUID <> String.Empty Then
            strSQL &= " and MainMediaGUID<>'" + strMainMediaGUID + "'"
        End If
        Try
            intCount = MyDB.GetDataItemInt(strSQL)
            If intCount > 0 Then
                Return "NO"
            Else
                Return "OK"
            End If
        Catch ex As Exception
            Return "操作失败.请与管理员联系！"
        End Try
    End Function



    '效验：判断媒体大类是否已被媒体子类引用
    Private Function MainMediaIsQuote(ByVal a_strPostValue As String) As String
        Dim strSQL As String = ""
        Dim intCount As Integer
        If a_strPostValue <> String.Empty Then
            strSQL = String.Format("SELECT COUNT(1) FROM s_SubMedia WHERE s_SubMedia.MainMediaGUID='{0}'", a_strPostValue)
        End If
        Try
            intCount = MyDB.GetDataItemInt(strSQL)
            If intCount > 0 Then
                Return "NO"
            Else
                Return "OK"
            End If
        Catch ex As Exception
            Return "操作失败.请与管理员联系！"
        End Try
    End Function

    '删除媒体大类
    Private Function DeleteMainMedia(ByVal a_strPostValue As String) As String
        Try
            Dim strSQL As String = ""
            '如果 Operate=define 则只删除折扣项，否则同时删除折扣方案和折扣项
            If a_strPostValue <> String.Empty Then
                strSQL = String.Format("DELETE s_MainMedia WHERE MainMediaGUID='{0}'", a_strPostValue)
            End If

            If MyDB.ExecSQL(strSQL) > -1 Then
                Return "SUCCESS"
            Else
                Return "操作失败.请与管理员联系!"
            End If
        Catch ex As Exception
            Return "操作失败.请与管理员联系!"
        End Try
    End Function


    '功能：修改媒体大类同时修改媒体子类中引用对应的的媒体大类编码
    Private Function UpdateSubMediaCode(ByVal a_strPostValue As String) As String
        '媒体大类GUID
        Dim strMainMediaGUID As String = MysRequest.QueryString("oid")
        '媒体大类编码
        Dim strMainMediaCode As String = MysRequest.QueryString("mainCode")
        Try
            Dim strSQL As String = ""
            If strMainMediaGUID <> String.Empty Then
                strSQL = String.Format("UPDATE s_SubMedia SET SubMediaCode='{0}'+SUBSTRING(SubMediaCode,CHARINDEX('.',SubMediaCode),100),FjCode='{0}'  WHERE MainMediaGUID='{1}'", strMainMediaCode, strMainMediaGUID)
            End If
            If MyDB.ExecSQL(strSQL) > -1 Then
                Return "SUCCESS"
            Else
                Return "操作失败.请与管理员联系!"
            End If
        Catch ex As Exception
            Return "操作失败.请与管理员联系!"
        End Try
    End Function

#End Region

End Class